
# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=MapServer
pkgbase=mingw-w64-${_realname,,}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname,,}")
pkgver=8.0.1
_pkgver=rel-${pkgver//./-}
pkgrel=3
pkgdesc='Platform for publishing spatial data and interactive mapping applications to the web (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://mapserver.org/'
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-protobuf-c"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-postgis"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("${_realname}-${_pkgver}.tar.gz::https://github.com/MapServer/MapServer/archive/${_pkgver}.tar.gz"
        'https://ftp.gnu.org/old-gnu/regex/regex-0.12.tar.gz'
        '0001-mapserver-8.0.0-find-iconv.patch'
        '0002-mapserver-8.0.0-sysconf-dir.patch'
        '0003-mapserver-8.0.0-msvc-flags.patch'
        '0004-mapserver-8.0.0-dest-relative.patch'
        '0005-mapserver-8.0.0-skip-install-cmake.patch')

sha256sums=('53a3157d75987e13d5f449fd53a83bf50ac331f3db21bd085e727c5c0aab9ecf'
            'f36e2d8d56bf15054a850128fcb2f51807706a92d3bb4b37ceadd731535ce230'
            '26a0503fe6fb02516910c624959c2de48d6574709e28d632fdf0177bad3edaf2'
            '88c2a9ef5c1aa8cc54e295714cfd8b2a0c4d1b8f59470f2aa86cd5234209786d'
            '7e063d9a315b8763e48e6ce2d3cd1e9b971a16613ee8a0b405084e3a9c73b807'
            '6d66fbbdda99b7c794f5f7f9885a02f0727424a15774eeaf4d99a99876a85cbb'
            'a3b821f2d104f7fb4ff64f343063adac20bb5016a68c23a38b06312c0fcdc403')

prepare() {
  cd "${srcdir}"/${_realname}-${_pkgver}

  patch -Np1 -i "${srcdir}"/0001-mapserver-8.0.0-find-iconv.patch
  patch -Np1 -i "${srcdir}"/0002-mapserver-8.0.0-sysconf-dir.patch
  patch -Np1 -i "${srcdir}"/0003-mapserver-8.0.0-msvc-flags.patch
  patch -Np1 -i "${srcdir}"/0004-mapserver-8.0.0-dest-relative.patch
  patch -Np1 -i "${srcdir}"/0005-mapserver-8.0.0-skip-install-cmake.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;--prefix=;--install-scripts=;--install-platlib=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_SYSCONFDIR="etc" \
      -DCMAKE_CXX_STANDARD=17 \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_{DYNAMIC,STATIC}=ON \
      -DINSTALL_CMAKE_DIR="lib/cmake/mapserver" \
      -DPROTOBUFC_COMPILER="${MINGW_PREFIX}/bin/protoc.exe" \
      -DREGEX_DIR="${srcdir}/regex-0.12" \
      -DWITH_FCGI=OFF \
      -DWITH_RSVG=ON \
      -DWITH_PYTHON=ON \
      -DPython_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
      ../${_realname}-${_pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  for _file in LICENSE.md flatgeobuf/LICENSE flatgeobuf/include/flatbuffers/LICENSE
  do
    install -Dm644 "${srcdir}/${_realname}-${_pkgver}/${_file}" \
      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname,,}/${_file}"
  done
}
